package cn.wolfcode.exception;

import cn.hutool.http.HttpUtil;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.UserLog;
import cn.wolfcode.util.UtilContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;
import java.util.Objects;

@Component //把当前类注入到spring容器中
@Aspect    //把当前类标识为一个切面供容器读取
public class AopConfig {

    /**
     * 指定切入点
     * 对贴有自定义注解@RequiredPermission的方法，进行切面
     */
    @Pointcut("@annotation(cn.wolfcode.util.RequiredPermission)")
    private void useMethod() {
    }

    /**
     * 对切入点进行增强
     */
    @Around(value = "useMethod()")
    public Object recordLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //创建日志实体类
        UserLog userLog = new UserLog();
        //获取当前登录用户
//        if (Objects.isNull(UtilContext.getCurrentUser())){
//            return proceedingJoinPoint.proceed();
//
//        }
        String name = UtilContext.getCurrentUser().getName();
        //获取当前ip地址
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String ip = attrs.getRequest().getRemoteAddr();
        //获取操作时间
        Date date = new Date();

        //执行处理方法（执行控制器中的方法）
        Object result = proceedingJoinPoint.proceed();
        //result是方法的返回值，直接return回去就会返回给springmvc处理
        return result;
    }


}